#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "config.h"
#include "login.h"

// 判断是否登录
bool is_logged(){
    if(current_user[0] == '\0'){
        printf("请先登录\n");
        return false;
    }

    return true;
}

void wait_a_key(){
    int c;
    // 清空输入缓冲区中的所有字符
    while ((c = getchar()) != '\n' && c != EOF) {
        ;
    }
    printf("-------------------------------------------------------------------------------------------\n");
    printf("按下任意键继续...\n");
    getchar();  // 等待用户输入一个字符

    return;
}

void show_menu(){

    printf("------------今日菜单------------\n");
    printf("%-10s%-12s%-8s%-6s\n", "序号", "菜名", "单价", "点赞数");
    for(int i = 0; i < MENU_SIZE; i++){
        printf("%-6d%-18s%-8.1f%-6d\n",
            menu_number[i],
            cuisines[i],
            price[i],
            number_of_likes[i]);
    }
    printf("--------------------------------\n");
    return;
}

// 实现订餐功能
void reservation(){

    if(!is_logged()){
        return;
    }
    else if(is_admin){
        printf("管理员无法使用订餐功能\n");
        return;
    }


    printf("输入订餐人姓名:");
    scanf("%s", user_name[num_sys]);
    printf("\n");

    show_menu();

    printf("请输入要点的菜品编号:");
    scanf("%d", &reservation_menu_number[num_sys]);
    printf("\n");

    printf("请输入需要的份数:");
    scanf("%d", &how_many[num_sys]);
    printf("\n");

    printf("请输入送餐时间:");
    scanf("%d", &delivery_time[num_sys]);
    printf("\n");

    printf("请输入送餐地址:");
    scanf("%s", delivery_address[num_sys]);
    printf("\n");

    printf("订餐成功！\n");
    printf("请确定订单信息正确：\n");
    printf("%s\t%d份\n", cuisines[reservation_menu_number[num_sys]-1], how_many[num_sys]);
    printf("送餐时间:%d时\n", delivery_time[num_sys]);
    printf("送餐地址:%s\n", delivery_address[num_sys]);
    printf("餐费:%.1lf元,配送费:%.1lf元,总计%.1lf元。\n", 
        price[reservation_menu_number[num_sys]-1] * how_many[num_sys], // 总餐费
        5.0, price[reservation_menu_number[num_sys]-1] * how_many[num_sys]+5.0); // 总餐费 + 配送费
    
    strcpy(reservation_userid[num_sys], current_user); // 记录该订单由哪个用户创建
    num_sys++;

    return;
}

// 实现查看订单功能
void show_order(){

    if(!is_logged()){
        return;
    }
    else if(is_admin){
        printf("-----------------------------------------------------------------------------------------\n");
        printf("序号\t订餐人\t餐品信息\t\t送餐时间\t送餐地址\t总金额\t订单状态\n");
        for(int i = 0; i < num_sys; i++){
            printf("%d\t%s\t%s %d份\t\t%d时\t\t%s\t\t%.1lf元\t%s\n", i+1, user_name[i], 
                cuisines[reservation_menu_number[i]-1], how_many[i], delivery_time[i],
                delivery_address[i], 
                price[reservation_menu_number[i]-1] * how_many[i]+5.0,
            is_done[i] ? "已完成" : "已预定");
        }
        return;
    }
    else{
        printf("-----------------------------------------------------------------------------------------\n");
        printf("序号\t订餐人\t餐品信息\t\t送餐时间\t送餐地址\t总金额\t订单状态\n");
        for(int i = 0; i < num_sys; i++){
            if(!strcmp(current_user, reservation_userid[i])){
                printf("%d\t%s\t%s %d份\t\t%d时\t\t%s\t\t%.1lf元\t%s\n", i+1, user_name[i], 
                    cuisines[reservation_menu_number[i]-1], how_many[i], delivery_time[i],
                    delivery_address[i], 
                    price[reservation_menu_number[i]-1] * how_many[i]+5.0,
                is_done[i] ? "已完成" : "已预定");
            }

        }
        return;
    }
}

// 实现签收订单的功能
void sign_an_order(){

    if(!is_logged()){
        return;
    }
    else if(is_admin){
        printf("管理员无法使用签收功能\n");
        return;
    }

    int sign_num = 0; // 记录要签收的订单号

    show_order();
    printf("选择要签收的订单号:");
    scanf("%d", &sign_num);
    printf("\n");
    if(!strcmp(current_user, reservation_userid[sign_num - 1])){
        is_done[sign_num - 1] = true;
        printf("订单签收成功\n");
    }
    else{
        printf("用户的订单不存在，请重新确认\n");
    }
    return;
}




void delete_order(){

    int del_num = 0; //记录要删除的订单号

    if(!is_logged()){
        return;
    }
    else if(is_admin){

        show_order();
        printf("选择要删除的订单号:");
        scanf("%d", &del_num);
        printf("\n");
    
        if(del_num > num_sys){
            printf("订单号不存在，请重试\n");
        }
        else if(!is_done[del_num - 1]){
            printf("订单未签收，无法删除，请重试\n");
        }
        else{
            // 把删除的订单号后的订单往前填补
            for(int i = del_num - 1; i < num_sys; i++){
                strcpy(user_name[i], user_name[i+1]);
                reservation_menu_number[i] = reservation_menu_number[i+1];
                how_many[i] = how_many[i+1];
                delivery_time[i] = delivery_time[i+1];
                strcpy(delivery_address[i], delivery_address[i+1]);
                is_done[i] = is_done[i+1];
                strcpy(reservation_userid[i], reservation_userid[i+1]);
            }
            num_sys--;
            printf("订单删除成功\n");
        }
    
        return;
    }
    else{
        show_order();
        printf("选择要删除的订单号:");
        scanf("%d", &del_num);
        printf("\n");
    
        if(!strcmp(current_user, reservation_userid[del_num - 1])){
            if(!is_done[del_num - 1]){
                printf("订单未签收，无法删除，请重试\n");
                return;
            }
            // 把删除的订单号后的订单往前填补
            for(int i = del_num - 1; i < num_sys; i++){
                strcpy(user_name[i], user_name[i+1]);
                reservation_menu_number[i] = reservation_menu_number[i+1];
                how_many[i] = how_many[i+1];
                delivery_time[i] = delivery_time[i+1];
                strcpy(delivery_address[i], delivery_address[i+1]);
                is_done[i] = is_done[i+1];
                strcpy(reservation_userid[i], reservation_userid[i+1]);
            }
            num_sys--;
            printf("订单删除成功\n");
            return;
        }
        else{
            printf("用户的订单号不存在，请重试\n");
            return;
        }
    }

}

// 实现点赞功能
void upvote(){

    if(!is_logged()){
        return;
    }
    else if(is_admin){
        printf("管理员无法使用点赞功能\n");
        return;
    }

    int like_num = 0; // 记录要点赞的订单号

    show_menu();
    printf("输入要点赞的序号:");
    scanf("%d", &like_num);
    printf("\n");
    number_of_likes[like_num - 1]++;
    printf("点赞成功，感谢您的支持\n");
    show_menu();

    return;
}

// 实现功能的选择和显示
void interface(){

    int sellection = 0; // 记录用户的操作选择
    do{
        printf("******************************\n");
        printf("\t1.我要订餐\n");
        printf("\t2.查看餐袋\n");
        printf("\t3.签收订单\n");
        printf("\t4.删除订单\n");
        printf("\t5.我要点赞\n");
        printf("\t6.登录系统\n");
        printf("\t7.退出系统\n");
        printf("******************************\n");
        printf("选择功能：");
        scanf("%d", &sellection);
        printf("\n");

        
        switch (sellection)
        {
        case 1:
            reservation();
            wait_a_key();
            break;
        case 2:
            show_order();
            wait_a_key();
            break;
        case 3:
            sign_an_order();
            wait_a_key();
            break;
        case 4:
            delete_order();
            wait_a_key();
            break;
        case 5:
            upvote();
            wait_a_key();
            break;
        case 6:
            login_interface();
            break;
        case 7:
            printf("系统退出，欢迎再次使用本系统!\n");
            wait_a_key();
            break;
        default:
            printf("输入错误，请重试\n");
            wait_a_key();
            break;
        }
    }
    while(sellection != 7);



    return;
}

